<?php
/**
 * 社团
 */
namespace backend\controllers;

use bc\controller\BackendController;
use backend\models\organization\OrganizationModel;
use bc\util\backend\Page;
use bc\util\Uploader;

class OrganizationController extends BackendController
{
	
	
	/**
	 * 列表页
	 */
	public function actionIndex()
	{
		$model = new OrganizationModel();
		
		$organizationName = $this->get('organization_name');
		
		$params = '1 = 1 ';
		
		if($organizationName)
		{
			$params = "`organization_name` LIKE '%{$organizationName}%'";
		}
		
		$page = $this->get('page', 1);
		$list = $model->getList($params, $page, self::PAGE_SIZE);
		
		$total = $model->getTotal($params);
		
		$pagination = new Page($total, self::PAGE_SIZE);
		
		$view['list'] = $list;
		$view['pagination'] = $pagination;
		
		$view['organization_name'] = $organizationName;
		
		return $this->render('index', $view);
	}
	
	/**
	 * 新增
	 */
	public function actionAdd()
	{
		return $this->render('add');	
	}
	
	
	/**
	 * 编辑
	 */
	public function actionEdit()
	{
		
		$id = $this->get('id');
		if(!$id)
			$this->redirect('/organization/index');
		
		$model = new OrganizationModel();
		$row = $model->getRowById($id);
		
		if(!$row)
			$this->redirect('/organization/index');
		
		$view['row'] = $row;
		
		return $this->render('edit', $view);
	}
	
	public function actionSave()
	{
		
		if($this->getRequest()->getMethod() === 'POST' )
		{
			$model = new OrganizationModel();
			$id = $this->post('id', 0);
			
			$data['organization_name'] = $this->post('organization_name', '');
			$data['proprieter_desc'] = $this->post('proprieter_desc', '');
			$data['public'] = $this->post('public', '');
			$data['t_sort'] = $this->post('t_sort', '');
			$data['description'] = $this->post('description', '');
			$data['status'] = $this->post('status', '1');
			$data['proprieter_name'] = $this->post('proprieter_name', '');
			
			//社长icon上传
			if($_FILES['proprieter_icon'])
			{
				$up = new Uploader( 'proprieter_icon' , $this->getUploadConfig() );
				$info = $up->getFileInfo();
				if($info['state'] === 'SUCCESS')
				{
					$data['proprieter_icon'] = str_replace(UPLOAD_PATH, '', $info['url']);
				}
					
			}
			
			
			//社团icon
			if($_FILES['icon'])
			{
				$up = new Uploader( 'icon' , $this->getUploadConfig() );
				$info = $up->getFileInfo();
				if($info['state'] === 'SUCCESS')
				{
					$data['icon'] = str_replace(UPLOAD_PATH, '', $info['url']);
				}
			}
			
			//入群二维码上传
			if($_FILES['join_qrcode'])
			{
				$up = new Uploader( 'join_qrcode' , $this->getUploadConfig() );
				$info = $up->getFileInfo();
				if($info['state'] === 'SUCCESS')
				{
					$data['join_qrcode'] = str_replace(UPLOAD_PATH, '', $info['url']);
				}
			}
			
			//发送二维码模版的图片
			if($_FILES['template_image'])
			{
				$up = new Uploader( 'template_image' , $this->getUploadConfig() );
				$info = $up->getFileInfo();
				if($info['state'] === 'SUCCESS')
				{
					$data['template_image'] = str_replace(UPLOAD_PATH, '', $info['url']);
				}
			}
			
			
			if($id)
			{
				$model->modify($data, $id);
			}
			else
			{
				$data['create_time'] = time();
				$model->add($data);
			}
			$output['msg'] = '操作成功!';
			
		}
		else 
		{
			$output['msg'] = '非法请求!';
		}
		$output['url'] = '/organization/index';
		$this->forward($output);
	}
	
	
	/**
	 * @return array
	 */
	private function getUploadConfig()
	{
		//上传文件目录
		$path = UPLOAD_PATH . 'upload/';
		//上传配置
		$config = array(
				"savePath" => $path ,             //存储文件夹
				"maxSize" => 100000 ,                   //允许的文件最大尺寸，单位KB
				"allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" )  //允许的文件格式
		);
			
		//背景保存在临时目录中
		$config[ "savePath" ] = $path;
		return $config;
	}
}

